package com.telesound.dataimport;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.telesound.dataimport.util.BeanUtil;
import com.telesound.dataimport.util.StringUtil;
/**
 * 数据导入列规则的封装类
 * @author zhangchaofeng
 * @version 1.0
 * @date Sep 23, 2011
 */
public class DataImportColumnRule{
	private String id;                     //唯一标识，uuid
	private Date createTime;               //创建时间
	private String tableRuleId;            //data_import_table_rule表的主键
	private boolean isPk;                  //是否主键
	private String produceMethod;          //生成方法
	private boolean isUnique;              //是否唯一，用来判定重复数据
	private String columnName;             //列名称
	private String headerName;             //对应excel列头
	private boolean required;              //是否必须导入
	private String dataType;               //数据类型，string/number/date
	private String dateFormat;             //日期格式
	private int maxLength;                 //最大允许长度
	private String produceMethodValue;     //生成方法值
	private String comments;               //备注
	private boolean fromList;              //数据来源于列表
	private String dataList;               //数据列表
	private boolean isDictionary;          //是否属于字典
	private String dicTableName;           //字典表名称
	private String dicTypeColumn;          //字典类型列的名称
	private String dicTypeValue;           //字典类型值
	private String dicRequiredColumn;      //获取值的列名称
	private String dicReferenceColumn;     //参考值的列名称
	private String dicCondition;           //字典条件
	private boolean enable;                //是否有效
	private String blank1;
	private String blank2;
	private String blank3;
	private String blank4;
	private String blank5;
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		} else {
			if(obj instanceof DataImportColumnRule) {
				DataImportColumnRule rule = (DataImportColumnRule)obj;
				if(this.tableRuleId==null||(this.columnName==null&&this.headerName==null)){
					return false;
				}
			    if(this.tableRuleId .equals(rule.getTableRuleId()) && (this.columnName
			    		.equals(rule.getColumnName())||this.headerName.equals(rule.getHeaderName()))) {
			    	return true;
			    }
			}
		}
		return false;
	}

	@Override
	public int hashCode() {
		return this.tableRuleId.hashCode()*this.columnName.hashCode()*this.headerName.hashCode();
	}
	
	public static Map<String,DataImportColumnRule> getColumnRuleMap(String tableRuleId){
		Map<String,DataImportColumnRule> columnRules=new HashMap<String,DataImportColumnRule>();
		List args=new ArrayList();
		args.add(tableRuleId);
		String sql="select id from data_import_column_rule where table_rule_id=? and enable=1 order by create_time asc";
		List columnRuleList=DBAccess.getManyMap(sql, args);
		for(int i=0;i<columnRuleList.size();i++){
			Map info=(Map)columnRuleList.get(i);
			DataImportColumnRule columnRule=getColumnRule(StringUtil.getStringValue(info.get("id")));
			if(columnRule!=null){
				String columnName=columnRule.getColumnName();
				String headerName=columnRule.getHeaderName();
				if(!StringUtil.hasValue(columnRule.getHeaderName())){
					columnRules.put(columnName, columnRule);
				}else{
					columnRules.put(headerName, columnRule);
				}
			}
		}
		return columnRules;
	}
	
	public static List<DataImportColumnRule> getColumnRuleList(String tableRuleId){
		List<DataImportColumnRule> columnRules=new ArrayList<DataImportColumnRule>();
		List args=new ArrayList();
		args.add(tableRuleId);
		String sql="select id from data_import_column_rule where table_rule_id=? and enable=1 order by create_time asc";
		List columnRuleList=DBAccess.getManyMap(sql, args);
		for(int i=0;i<columnRuleList.size();i++){
			Map info=(Map)columnRuleList.get(i);
			DataImportColumnRule columnRule=getColumnRule(StringUtil.getStringValue(info.get("id")));
			if(columnRule!=null){
				columnRules.add(columnRule);
			}
		}
		return columnRules;
	}
	
	public static DataImportColumnRule getColumnRule(String columnRuleId){
		List args=new ArrayList();
		args.add(columnRuleId);
		String sql="select * from data_import_column_rule where id=?";
		Map ruleInfo=DBAccess.getSingleMap(sql, args);
		if(ruleInfo==null||ruleInfo.size()==0){
			return null;
		}
		DataImportColumnRule rule=new DataImportColumnRule();
		rule.setId(columnRuleId);
		rule=(DataImportColumnRule)BeanUtil.setProperties(rule, ruleInfo);
		return rule;
	}

	public String getBlank1() {
		return blank1;
	}

	public void setBlank1(String blank1) {
		this.blank1 = blank1;
	}

	public String getBlank2() {
		return blank2;
	}

	public void setBlank2(String blank2) {
		this.blank2 = blank2;
	}

	public String getBlank3() {
		return blank3;
	}

	public void setBlank3(String blank3) {
		this.blank3 = blank3;
	}

	public String getBlank4() {
		return blank4;
	}

	public void setBlank4(String blank4) {
		this.blank4 = blank4;
	}

	public String getBlank5() {
		return blank5;
	}

	public void setBlank5(String blank5) {
		this.blank5 = blank5;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTableRuleId() {
		return tableRuleId;
	}

	public void setTableRuleId(String tableRuleId) {
		this.tableRuleId = tableRuleId;
	}

	public boolean getIsUnique() {
		return isUnique;
	}

	public void setIsUnique(boolean isUnique) {
		this.isUnique = isUnique;
	}

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public String getHeaderName() {
		return headerName;
	}

	public void setHeaderName(String headerName) {
		this.headerName = headerName;
	}

	public boolean getRequired() {
		return required;
	}

	public void setRequired(boolean required) {
		this.required = required;
	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public boolean getFromList() {
		return fromList;
	}

	public void setFromList(boolean fromList) {
		this.fromList = fromList;
	}

	public String getDataList() {
		return dataList;
	}

	public void setDataList(String dataList) {
		this.dataList = dataList;
	}

	public boolean getIsPk() {
		return isPk;
	}

	public void setIsPk(boolean isPk) {
		this.isPk = isPk;
	}

	public String getProduceMethod() {
		return produceMethod;
	}

	public void setProduceMethod(String produceMethod) {
		this.produceMethod = produceMethod;
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}

	public String getProduceMethodValue() {
		return produceMethodValue;
	}

	public void setProduceMethodValue(String produceMethodValue) {
		this.produceMethodValue = produceMethodValue;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public boolean getIsDictionary() {
		return isDictionary;
	}

	public void setIsDictionary(boolean isDictionary) {
		this.isDictionary = isDictionary;
	}

	public String getDicTableName() {
		return dicTableName;
	}

	public void setDicTableName(String dicTableName) {
		this.dicTableName = dicTableName;
	}

	public int getMaxLength() {
		return maxLength;
	}

	public void setMaxLength(int maxLength) {
		this.maxLength = maxLength;
	}

	public String getDicTypeColumn() {
		return dicTypeColumn;
	}

	public void setDicTypeColumn(String dicTypeColumn) {
		this.dicTypeColumn = dicTypeColumn;
	}

	public String getDicTypeValue() {
		return dicTypeValue;
	}

	public void setDicTypeValue(String dicTypeValue) {
		this.dicTypeValue = dicTypeValue;
	}

	public void setPk(boolean isPk) {
		this.isPk = isPk;
	}

	public String getDicRequiredColumn() {
		return dicRequiredColumn;
	}

	public void setDicRequiredColumn(String dicRequiredColumn) {
		this.dicRequiredColumn = dicRequiredColumn;
	}

	public String getDicReferenceColumn() {
		return dicReferenceColumn;
	}

	public void setDicReferenceColumn(String dicReferenceColumn) {
		this.dicReferenceColumn = dicReferenceColumn;
	}

	public boolean getEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getDicCondition() {
		return dicCondition;
	}

	public void setDicCondition(String dicCondition) {
		this.dicCondition = dicCondition;
	}
}
